#include "AppManager.h"
#include "DS3231.h"
extern DS3231 Srtc;

static const uint8_t clock_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0xfe, 0x7f, 0x00,
   0x80, 0x1f, 0xf8, 0x01, 0xc0, 0x03, 0xe0, 0x03, 0xe0, 0x01, 0x80, 0x07,
   0xf0, 0x00, 0x00, 0x0f, 0x78, 0x80, 0x01, 0x1e, 0x38, 0x80, 0x01, 0x1c,
   0x1c, 0x80, 0x01, 0x38, 0x1c, 0x80, 0x01, 0x30, 0x0e, 0x80, 0x01, 0x70,
   0x0e, 0x80, 0x01, 0x70, 0x06, 0x80, 0x01, 0x60, 0x06, 0x80, 0x01, 0x60,
   0x06, 0x80, 0x01, 0x60, 0x06, 0x80, 0x03, 0x60, 0x06, 0x80, 0x07, 0x60,
   0x06, 0x00, 0x0e, 0x60, 0x0e, 0x00, 0x1c, 0x70, 0x0e, 0x00, 0x38, 0x30,
   0x1c, 0x00, 0x10, 0x38, 0x1c, 0x00, 0x00, 0x38, 0x38, 0x00, 0x00, 0x1c,
   0x78, 0x00, 0x00, 0x1e, 0xf0, 0x00, 0x00, 0x0f, 0xe0, 0x01, 0x80, 0x07,
   0xc0, 0x07, 0xe0, 0x03, 0x80, 0x1f, 0xf8, 0x01, 0x00, 0xfe, 0x7f, 0x00,
   0x00, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00 };
class AppClockOnly : public AppBase
{
private:
    /* data */
public:
    AppClockOnly()
    {
        name = "clockonly";
        title = "仅时钟";
        description = "仅时钟模式";
        image = clock_bits;
        peripherals_requested = PERIPHERALS_AHT20_BIT;
    }
    void setup();
};
static AppClockOnly app;
extern const char *dayOfWeek[];
void AppClockOnly::setup()
{
    int w;
    char timeStr[6];
    if(config[Clocksource] == "1")
    {
        int h = Srtc.getHour();
        int m = Srtc.getMinute();
        sprintf(timeStr, "%02d:%02d", h, m);  
    }
    else
    {
        sprintf(timeStr, "%02d:%02d", hal.timeinfo.tm_hour, hal.timeinfo.tm_min);
    }
    sensors_event_t humidity, temp;
    peripherals.aht.getEvent(&humidity, &temp);
    display.clearScreen();
    u8g2Fonts.setFontMode(1);
    u8g2Fonts.setForegroundColor(0);
    u8g2Fonts.setBackgroundColor(1);
    u8g2Fonts.setFont(u8g2_font_logisoso92_tn);
    w = u8g2Fonts.getUTF8Width(timeStr);
    u8g2Fonts.setCursor((296 - w) / 2, 104);
    u8g2Fonts.print(timeStr);
    u8g2Fonts.setFont(u8g2_font_wqy12_t_gb2312);
    display.drawFastHLine(0, 110, 296, 0);
    u8g2Fonts.setCursor(10, 125);
    if(config[Clocksource] == "1")
    {
        u8g2Fonts.printf("%02d月%02d日 星期%s  温度: %.2f℃ 湿度：%.2f% rH\n",  Srtc.getMonth(), Srtc.getDate(), dayOfWeek[hal.timeinfo.tm_wday], temp.temperature,humidity.relative_humidity);
    }
    else
    {
        u8g2Fonts.printf("%02d月%02d日 星期%s  温度: %.2f℃ 湿度：%.2f% rH\n",  hal.timeinfo.tm_mon + 1, hal.timeinfo.tm_mday, dayOfWeek[hal.timeinfo.tm_wday], temp.temperature,humidity.relative_humidity);
    }   
    // 电池
    display.drawXBitmap(296 - 25, 111, getBatteryIcon(), 20, 16, 0);

    if (force_full_update || part_refresh_count > 20)
    {
        display.display(false);
        force_full_update = false;
        part_refresh_count = 0;
    }
    else
    {
        display.display(true);
        part_refresh_count++;
    }
    appManager.noDeepSleep = false;
    if(config[Clocksource] == "1")
    {
        appManager.nextWakeup = 61 - Srtc.getSecond();
        Serial.printf("DS3231已启用  DS3231数据：20%d年%d月%d日  %d:%d:%d\n",Srtc.getYear(),Srtc.getMonth(),Srtc.getDate(),Srtc.getHour(),Srtc.getMinute(),Srtc.getSecond());
    }
    else{
        appManager.nextWakeup = 61 - hal.timeinfo.tm_sec;
        Serial.printf("DS3231未启用  DS3231数据：20%d年%d月%d日  %d:%d:%d\n",Srtc.getYear(),Srtc.getMonth(),Srtc.getDate(),Srtc.getHour(),Srtc.getMinute(),Srtc.getSecond());
    }
    Serial.printf("ESP32RTC数据：20%d年%d月%d日  %d:%d:%d\n",hal.timeinfo.tm_year,hal.timeinfo.tm_mon + 1,hal.timeinfo.tm_mday,hal.timeinfo.tm_hour,hal.timeinfo.tm_min,hal.timeinfo.tm_sec);
    Serial.printf("ESP32RTC与DS3231差值：%ds(DS3231-ESP32RTC)\n",Srtc.getSecond()-hal.timeinfo.tm_sec);
}
